using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace Advanced_Terrain
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class KeyboardHandling : DrawableGameComponent
    {
        public static WiiMoteConnector MyWiimote = new WiiMoteConnector();
        MouseState originalMouseState;
        const float rotationSpeed = 0.3f;
        const float moveSpeed = 30.0f;
        bool menuUp = false;
        bool menuDown = false;
        bool menuEnter = false;
        int ViewPortWidth;
        int ViewPortHeight;
        float leftrightRot = MathHelper.PiOver2;
        float updownRot = -MathHelper.Pi / 10.0f;
        Vector3 WiiMotePowers;
        Vector3 WiiMoteAngles;
        KeyboardState PreviousKeyState;
        public static float LastDistance = 10000f;
        


        public KeyboardHandling(Game game, int ViewPortWidthin, int ViewPortHeightin)
            : base(game)
        {
            ViewPortHeight = ViewPortHeightin;
            ViewPortWidth = ViewPortWidthin;
        }
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        protected override void LoadContent()
        {
            Mouse.SetPosition(ViewPortWidth / 2, ViewPortHeight / 2);
            originalMouseState = Mouse.GetState();
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }


        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here




            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
        public void ProcessInput(float amount, GameTime gameTime)
        {
            KeyboardState keyState;
            MouseState currentMouseState;
            Vector3 moveVector = new Vector3(0, 0, 0);
            keyState = Keyboard.GetState();
            float WiimoteCorrector = .025f;
            switch (Game1.GameState)
            {
                case Game1.GameStateWiiMoteCalc:
                    {
                        Game1.newDisc.SetDiscInfo(Game1.DiscOptions.DiscInfo[Game1.Players[Game1.CurrentPlayer].LastDiscIndex]);
                        Game1.newDisc.BalancingCoeffiecent *= Game1.Players[Game1.CurrentPlayer].ThrowStyle;
                        Game1.newDisc.Pitch = updownRot;
                        Game1.newDisc.Yaw = leftrightRot;
                        Game1.newDisc.Roll = MathHelper.ToRadians(MyWiimote.Angle.Z * -1);
                        float NewVelocity;
                        NewVelocity = Math.Abs(WiiMotePowers.X) / 20;
                        if (NewVelocity > 1)
                        {
                            NewVelocity = 1.2f;
                        }
                        NewVelocity *= 100;
                        //if (abs(WiiMotePowers.X) > 4)
                        //    NewVelocity = 100;
                        //else
                        //    NewVelocity = 100 - ((4 -  Math.Abs( WiiMotePowers.X)/100) * 10);
                        NewVelocity *= Game1.newDisc.PowerMultiplier;
                        Game1.newDisc.Velocity = new Vector3(0, 0, NewVelocity);
                        Game1.newDisc.Flying = true;
                        
                        Game1.newDisc.SpinFactor = 1f;
                        Game1.GameState = Game1.GameStateFlying;
                        CameraProperties.CameraFloating = CameraProperties.CameraDiskFlying;
                        break;


                    }
                case Game1.GameStateAiming:
                    {
                        currentMouseState = Mouse.GetState();
                        if (currentMouseState != originalMouseState)
                        {
                            float xDifference = currentMouseState.X - originalMouseState.X;
                            float yDifference = currentMouseState.Y - originalMouseState.Y;
                            leftrightRot -= rotationSpeed * xDifference * amount;
                            updownRot -= rotationSpeed * yDifference * amount;

                        }
                        if (MyWiimote.wm.WiimoteState.ButtonState.Left)
                        {
                            leftrightRot += rotationSpeed * WiimoteCorrector;


                        }
                        if (MyWiimote.wm.WiimoteState.ButtonState.Right)
                        {
                            leftrightRot -= rotationSpeed * WiimoteCorrector;
                        }

                        if (MyWiimote.wm.WiimoteState.ButtonState.Up)
                            updownRot += rotationSpeed * WiimoteCorrector;
                        if (MyWiimote.wm.WiimoteState.ButtonState.Down)
                            updownRot -= rotationSpeed * WiimoteCorrector;
                        /*
                        if (keyState.IsKeyDown(Keys.Q))
                        {
                            Advanced_Terrain.CameraProperties.CameraFloating = CameraProperties.CameraFloatingYes;
                            Game1.GameState = GameStateFlying;
                            CameraProperties.cameraPosition.Y += 2f;

                        }*/
                        if (keyState.IsKeyDown(Keys.Space))
                        {
                            if (Game1.PowerDelay < 0)
                            {
                                Game1.PowerSwitch = false;
                                Game1.PowerCounter = 0;
                                Game1.PowerDelay = 50;
                                Game1.GameState = Game1.GameStateVertPower;
                            }
                            Game1.PowerDelay--;
                        }
                        if (MyWiimote.wm.WiimoteState.ButtonState.A)
                        {
                            Game1.GameState = Game1.GameStateWiiMoteSwinging;
                        }
                        if (keyState.IsKeyDown(Keys.Z) || MyWiimote.wm.WiimoteState.ButtonState.One)
                        {
                            SetLeftRightRot();
                            LastDistance = 10000f;
                            Game1.GameState = Game1.GameStateZoomingToBasket;
                            CameraProperties.CameraFloating = CameraProperties.CameraZooming;
                            CameraProperties.cameraPosition.Y += 5f;
                        }
                        if (keyState.IsKeyDown(Keys.C) || MyWiimote.wm.WiimoteState.ButtonState.Two)
                        {
                            Game1.DiscSelection.Update();
                            Game1.DiscSelection.Visible = true;
                            Game1.GameState = Game1.GameStateChooseDisc;
                        }
                        else
                        {
                            Game1.DiscSelection.Visible = false;
                        }
                        if (updownRot > (Math.PI / 180) * 45)
                            updownRot = (float)(Math.PI / 180) * 45f;
                        if (updownRot < (Math.PI / 180 * -45))
                            updownRot = (float)(Math.PI / 180) * -45f;
                        Mouse.SetPosition(ViewPortWidth / 2, ViewPortHeight / 2);
                        UpdateViewMatrix();

                        AddToCameraPosition(moveVector * amount, gameTime);
                        break;

                    }
                case Game1.GameStateChooseDisc:
                    {
                        if (keyState.IsKeyDown(Keys.Down) || MyWiimote.wm.WiimoteState.ButtonState.Down)
                            if (menuDown)
                            {
                                Game1.DiscSelection.NextItem();
                                menuDown = false;
                            }
                        if (MyWiimote.IsConnected)
                        {
                            if (!MyWiimote.wm.WiimoteState.ButtonState.Down)
                                menuDown = true;
                        }
                        else
                        {
                            if (keyState.IsKeyUp(Keys.Down))
                                menuDown = true;
                        }
                        if (keyState.IsKeyDown(Keys.Up) || MyWiimote.wm.WiimoteState.ButtonState.Up)
                            if (menuUp)
                            {
                                Game1.DiscSelection.PrevItem();
                                menuUp = false;
                            }
                        if (MyWiimote.IsConnected)
                        {
                            if (!MyWiimote.wm.WiimoteState.ButtonState.Up)
                            {
                                menuUp = true;
                            }
                        }
                        else
                        {
                            if (keyState.IsKeyUp(Keys.Up))
                                menuUp = true;
                        }
                        if (keyState.IsKeyDown(Keys.Enter) || MyWiimote.wm.WiimoteState.ButtonState.A)
                        {
                            if (menuEnter)
                            {
                                Game1.DiscSelection.SelectCurrent();
                                menuEnter = false;
                                Game1.Players[Game1.CurrentPlayer].LastDiscIndex = Game1.DiscSelection.DiscIndex;
                                //newDisc.SetDiscInfo(DiscOptions.DiscInfo[DiscSelection.DiscIndex]);
                                Game1.GameState = Game1.GameStateAiming;
                            }
                        }
                        if (keyState.IsKeyUp(Keys.Enter) || !MyWiimote.wm.WiimoteState.ButtonState.A)
                            menuEnter = true;
                        break;


                    }

                case Game1.GameStateZoomingToBasket:
                    {
                        if (!keyState.IsKeyDown(Keys.Z) || MyWiimote.wm.WiimoteState.ButtonState.One)
                        {
                            Game1.GameState = Game1.GameStateAiming;

                        }
                        else
                        {
                            if (LastDistance > Vector3.Distance(CameraProperties.cameraPosition, Game1.newDisc.BasketPosition))
                            {
                                LastDistance = Vector3.Distance(CameraProperties.cameraPosition, Game1.newDisc.BasketPosition);
                                moveVector += new Vector3(0, 0, -.5f) * Vector3.Distance(CameraProperties.cameraPosition, Game1.newDisc.BasketPosition);
                                AddToCameraPosition(moveVector * amount, gameTime);

                            }
                            else
                            {
                                moveVector = new Vector3(0, 2f, 0);
                                AddToCameraPosition(moveVector * amount, gameTime);

                            }
                        }

                        break;
                    }
                case Game1.GameStateDisplayingScores:
                    {

                        if (keyState.IsKeyDown(Keys.Enter) || MyWiimote.wm.WiimoteState.ButtonState.A)
                            Game1.GameState = Game1.GameStateInBasket;
                        break;
                    }
                case Game1.GameStateMenu:
                    {
                        if (keyState.IsKeyDown(Keys.Down) || MyWiimote.wm.WiimoteState.ButtonState.Down)
                        {
                            if (menuDown)
                            {
                                Game1.mainMenu.NextItem();
                                menuDown = false;
                            }
                        }
                        if (MyWiimote.IsConnected)
                        {
                            if (!MyWiimote.wm.WiimoteState.ButtonState.Down)
                            {
                                menuDown = true;
                            }
                        }
                        else
                        {
                            if (keyState.IsKeyUp(Keys.Down))
                                menuDown = true;
                        }
                        if (keyState.IsKeyDown(Keys.Up) || MyWiimote.wm.WiimoteState.ButtonState.Up)
                        {
                            if (menuUp)
                            {
                                Game1.mainMenu.PrevItem();
                                menuUp = false;
                            }
                        }
                        if (MyWiimote.IsConnected)
                        {
                            if (!MyWiimote.wm.WiimoteState.ButtonState.Up)
                                menuUp = true;

                        }
                        else
                        {
                            if (keyState.IsKeyUp(Keys.Up))
                                menuUp = true;
                        }
                        if (keyState.IsKeyDown(Keys.Enter) || MyWiimote.wm.WiimoteState.ButtonState.A)
                        {
                            if (menuEnter)
                            {
                                Game1.mainMenu.SelectCurrent();
                                menuEnter = false;
                            }
                        }
                        if (MyWiimote.IsConnected)
                        {
                            if (!MyWiimote.wm.WiimoteState.ButtonState.A)
                                menuEnter = true;
                        }
                        else
                        {
                            if (keyState.IsKeyUp(Keys.Enter))
                                menuEnter = true;
                        }
                        break;
                    }

                case Game1.GameStateVertPower:
                    {
                        if (Game1.PowerDelay < 1)
                        {
                            if (keyState.IsKeyDown(Keys.Space))
                            {
                                Game1.newDisc.SetDiscInfo(Game1.DiscOptions.DiscInfo[Game1.Players[Game1.CurrentPlayer].LastDiscIndex]);
                                Game1.newDisc.BalancingCoeffiecent *= Game1.Players[Game1.CurrentPlayer].ThrowStyle;
                                //newDisc.ResetDisc();
                                Game1.newDisc.Pitch = updownRot;
                                Game1.newDisc.Yaw = leftrightRot;

                                float NewVelocity;
                                if (Game1.PowerCounter > 252)
                                    NewVelocity = 100;
                                else
                                {
                                    NewVelocity = 100 - (((252 - Game1.PowerCounter) / 6) * 2);
                                }
                                NewVelocity = 100;
                                NewVelocity *= Game1.newDisc.PowerMultiplier;
                                Game1.newDisc.Velocity = new Vector3(0, 0, NewVelocity);
                                Game1.PowerCounter = 0;
                                Game1.PowerSwitch = false;
                                Game1.PowerDelay = 50;
                                Game1.GameState = Game1.GameStateHorPower;


                            }
                        }
                        Game1.PowerDelay--;
                        break;
                    }
                case Game1.GameStateHorPower:
                    {
                        if (Game1.PowerDelay < 1)
                        {
                            if (keyState.IsKeyDown(Keys.Space))
                            {
                                float NewAngle;
                                if (Game1.PowerCounter == 0)
                                    NewAngle = 0;
                                else if (Game1.PowerCounter < 128)
                                {
                                    NewAngle = (128 - Game1.PowerCounter) / 3;
                                }
                                else
                                {
                                    NewAngle = ((Game1.PowerCounter - 128) / 3) * -1;
                                }

                                Game1.newDisc.Roll = MathHelper.ToRadians(NewAngle);
                                Game1.newDisc.ApplyRotations();
                                Game1.newDisc.Flying = true;
                                Game1.newDisc.SpinFactor = 1.3f;
                                Game1.PowerCounter = 0;
                                Game1.PowerSwitch = false;
                                Game1.PowerDelay = 10;
                                Game1.GameState = Game1.GameStateFlying;
                                CameraProperties.CameraFloating = CameraProperties.CameraDiskFlying;


                            }
                        }
                        Game1.PowerDelay--;
                        break;
                    }

                default:
                    {
                        if (Game1.GameState == Game1.GameStateNoCameraMove)
                        {
                            CameraProperties.CameraFloating = CameraProperties.CameraNotMoving;
                        }
                        AddToCameraPosition(moveVector * amount, gameTime);
                        break;
                    }

                




            }
        }

        private void AddToCameraPosition(Vector3 vectorToAdd, GameTime gameTime)
        {
            switch (CameraProperties.CameraFloating)
            {
                case CameraProperties.CameraFloatingYes:
                    Vector3 rotatedVector;
                    Game1.cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
                    rotatedVector = Vector3.Transform(vectorToAdd, Game1.cameraRotation);
                    Advanced_Terrain.CameraProperties.cameraPosition += moveSpeed * rotatedVector;


                    if (Advanced_Terrain.CameraProperties.cameraPosition.X < 0 + Game1.TerrainAdjuster)
                        Advanced_Terrain.CameraProperties.cameraPosition.X = 0 + Game1.TerrainAdjuster;
                    if (Advanced_Terrain.CameraProperties.cameraPosition.X > Game1.newTerrain.terrainWidth - Game1.TerrainAdjuster)
                        Advanced_Terrain.CameraProperties.cameraPosition.X = Game1.newTerrain.terrainWidth - Game1.TerrainAdjuster;
                    if (Advanced_Terrain.CameraProperties.cameraPosition.Z > 0 - Game1.TerrainAdjuster)
                        Advanced_Terrain.CameraProperties.cameraPosition.Z = 0 - Game1.TerrainAdjuster;
                    if (Advanced_Terrain.CameraProperties.cameraPosition.Z < (Game1.newTerrain.terrainLength * -1) + Game1.TerrainAdjuster)
                        Advanced_Terrain.CameraProperties.cameraPosition.Z = (Game1.newTerrain.terrainLength * -1) + Game1.TerrainAdjuster;
                    if (Advanced_Terrain.CameraProperties.cameraPosition.Y < 0)
                        if (Advanced_Terrain.CameraProperties.cameraPosition.Y < 0)
                            Advanced_Terrain.CameraProperties.cameraPosition.Y = 0;
                    Game1.TerrainHeight = Game1.newTerrain.GetTerrainHeight(Advanced_Terrain.CameraProperties.cameraPosition.X, Advanced_Terrain.CameraProperties.cameraPosition.Z * -1) + 2.0f;

                    if (Game1.TerrainHeight < 5)
                        Advanced_Terrain.CameraProperties.cameraPosition.Y = 5;
                    if (Advanced_Terrain.CameraProperties.cameraPosition.Y < Game1.TerrainHeight)
                    {
                        Advanced_Terrain.CameraProperties.cameraPosition.Y = Game1.TerrainHeight - 1;
                    }

                    break;

                case CameraProperties.CameraFloatingNo:

                    //float TempFactor;
                    //TempFactor = 1 / (newDisc.Direction.X + (-1 * newDisc.Direction.Z));
                    //TempFactor = TempFactor * 7;
                    CameraProperties.cameraPosition = Game1.newDisc.Position - (Game1.newDisc.Direction * 30);
                    CameraProperties.cameraPosition.Y += 2.5f;
                    //cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);

                    break;
                case CameraProperties.CameraFloatingOnDisk:

                    CameraProperties.cameraPosition = Game1.newDisc.Position;
                    CameraProperties.cameraPosition.Y += .4f;
                    Game1.cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);

                    break;
                case CameraProperties.CameraDeflection:

                    break;
                case CameraProperties.CameraDiskFlying:
                    //Multiplied by positive values makes a springy chase camera.  Multiply by negatives makes a reverse lookin camera
                    CameraProperties.cameraPosition = Game1.newDisc.Position - (Game1.newDisc.Direction * 250 * Math.Abs(Game1.newDisc.Direction.Z));
                    float TestHeight = Game1.newDisc.TerrainHeight;
                    if (CameraProperties.cameraPosition.Y < TestHeight)
                    {
                        CameraProperties.cameraPosition.Y = TestHeight;
                    }
                    CameraProperties.cameraPosition.Y += 1.3f;
                    
                    break;
                case CameraProperties.CameraCloseToBasket:

                    Vector3 Multiplier;
                    Multiplier = Game1.newBasket.Position - Game1.newDisc.Position;
                    //Multiplier.Y = newBasket.Position.Y + 2f;
                    if (Multiplier.Y < 0)
                        Multiplier.Y *= -1;
                    Multiplier.Y = Multiplier.Y + 2f;



                    //Multiplier.Y += .1f;

                    CameraProperties.cameraPosition = Game1.newBasket.Position + Multiplier * 2;
                    CameraProperties.cameraPosition.Y = CameraProperties.cameraPosition.Y + 1f;
                    try
                    {
                        if (CameraProperties.cameraPosition.Y < Game1.newDisc.TerrainHeight)
                            CameraProperties.cameraPosition.Y = Game1.newDisc.TerrainHeight + 2f;
                    }
                    catch
                    {
                        CameraProperties.cameraPosition.Y = Game1.newDisc.TerrainHeight + 2f;
                    }




                    break;
                case CameraProperties.CameraAnimatingChains:
                    //CameraProperties.cameraPosition = newDisc.Position - (newDisc.Direction * 10);



                    break;
                case CameraProperties.CameraZooming:
                    CameraProperties.cameraPosition += Vector3.Transform(vectorToAdd, Game1.cameraRotation);

                    break;
                case CameraProperties.CameraNotMoving:
                    CameraProperties.cameraPosition.Y = Game1.newDisc.TerrainHeight + 4f;
                    break;


            }
            UpdateViewMatrix();
        }

        public void SetLeftRightRot()
        {
            Game1.cameraRotation = Matrix.CreateFromQuaternion(Game1.newDisc.Rotation);
            Vector3 OriginalPos = Game1.newDisc.Position;
            float Closest = 99999;
            float Distance= 0;
            float RightX = -1;
            float PreviousDistance = -99999;
            for (int X = 0; X < 360; X = X + 3)
            {
                Game1.newDisc.Yaw = MathHelper.ToRadians(X);
                Game1.newDisc.ApplyRotations();
                Game1.newDisc.Position += Vector3.Transform(new Vector3(0, 0, -10), Game1.newDisc.Rotation);
                Distance = Vector3.Distance(Game1.newDisc.Position, Game1.newBasket.Position);
                if (Distance < Closest)
                {
                    Closest = Distance;
                    RightX = X;
                }
               
                PreviousDistance = Distance;
                Game1.newDisc.ResetDisc();
                Game1.newDisc.Position = OriginalPos;
            }
            leftrightRot = MathHelper.ToRadians(RightX);
        
        }

        public void UpdateViewMatrix()
        {
                switch (Advanced_Terrain.CameraProperties.CameraFloating)
                {
                    case CameraProperties.CameraFloatingYes:
                        {
                            Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
                            Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
                            Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
                            Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
                            Vector3 cameraFinalTarget = Advanced_Terrain.CameraProperties.cameraPosition + cameraRotatedTarget;
                            Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);
                            CameraProperties.CameraUp = cameraRotatedUpVector;
                            Advanced_Terrain.CameraProperties.cameraTarget = cameraFinalTarget;
                            Game1.viewMatrix = Matrix.CreateLookAt(Advanced_Terrain.CameraProperties.cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
                            CameraProperties.View = Game1.viewMatrix;
                            break;
                        }
                    case CameraProperties.CameraFloatingNo:
                        {
                            Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
                            Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
                            Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
                            Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
                            Vector3 cameraFinalTarget = Advanced_Terrain.CameraProperties.cameraPosition + cameraRotatedTarget;
                            Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);
                            CameraProperties.CameraUp = cameraRotatedUpVector;
                            CameraProperties.cameraTarget = cameraFinalTarget;
                            Advanced_Terrain.CameraProperties.cameraTarget = cameraFinalTarget;
                            Game1.viewMatrix = Matrix.CreateLookAt(Advanced_Terrain.CameraProperties.cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
                            CameraProperties.View = Game1.viewMatrix;
                            //CameraProperties.CameraFloating = CameraProperties.CameraDiskFlying;

                            break;
                        }

                    case CameraProperties.CameraDiskFlying:
                        {
                            Game1.viewMatrix = Matrix.CreateLookAt(Advanced_Terrain.CameraProperties.cameraPosition, Game1.newDisc.Position, Vector3.Up);
                            CameraProperties.View = Game1.viewMatrix;
                            break;
                        }
                    case CameraProperties.CameraFloatingOnDisk:
                        {
                            Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
                            Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
                            Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
                            Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
                            Vector3 cameraFinalTarget = Advanced_Terrain.CameraProperties.cameraPosition + cameraRotatedTarget;
                            Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);
                            CameraProperties.CameraUp = cameraRotatedUpVector;
                            Advanced_Terrain.CameraProperties.cameraTarget = cameraFinalTarget;
                            Game1.viewMatrix = Matrix.CreateLookAt(Advanced_Terrain.CameraProperties.cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
                            CameraProperties.View = Game1.viewMatrix;
                            break;
                        }
                    case CameraProperties.CameraDeflection:
                        {
                            CameraProperties.cameraTarget = Game1.newDisc.Position;
                            Game1.viewMatrix = Matrix.CreateLookAt(Advanced_Terrain.CameraProperties.cameraPosition, Game1.newDisc.Position, Vector3.Up);
                            CameraProperties.View = Game1.viewMatrix;
                            break;
                        }
                    case CameraProperties.CameraCloseToBasket:
                        {
                            //newBasket.IsVisible = false;
                            CameraProperties.cameraTarget = Game1.newDisc.Position;
                            Game1.viewMatrix = Matrix.CreateLookAt(Advanced_Terrain.CameraProperties.cameraPosition, Game1.newDisc.Position, Vector3.Up);
                            CameraProperties.View = Game1.viewMatrix;
                            break;
                        }
                    case CameraProperties.CameraAnimatingChains:
                        {
                            /*Random RandomAngle;
                            RandomAngle = new Random();
                            CameraProperties.cameraTarget = newDisc.Position;
                            CameraProperties.cameraPosition = newDisc.BasketPosition;
                            CameraProperties.cameraPosition.Y = newDisc.Position.Y;
                            //CameraProperties.cameraTarget.X = RandomAngle.Next(0, 100);
                            //CameraProperties.cameraTarget.Y = RandomAngle.Next(0, 100);
                            if (FirstTime)
                            {
                                CameraProperties.cameraPosition += new Vector3(RandomAngle.Next(-1, 1), RandomAngle.Next(-1, 1), RandomAngle.Next(-1, 1)) * .0005f;
                                FirstTime = false;
                            }
                            else
                            {
                                FirstTime = true;
                            }*/

                            //viewMatrix = Matrix.CreateLookAt(Advanced_Terrain.CameraProperties.cameraPosition, newDisc.Position + (new Vector3(RandomAngle.Next(-1,1),RandomAngle.Next(-1,1),RandomAngle.Next(-1,1)) * .1f), Vector3.Up);
                            //viewMatrix = Matrix.CreateLookAt(Advanced_Terrain.CameraProperties.cameraPosition, newDisc.Position + (new Vector3(RandomAngle.Next(-1, 1), RandomAngle.Next(-1, 1), RandomAngle.Next(-1, 1)) * .05f), Vector3.Up);
                            Game1.viewMatrix = Matrix.CreateLookAt(CameraProperties.cameraPosition, Game1.newDisc.Position, Vector3.Up);
                            CameraProperties.View = Game1.viewMatrix;
                            break;
                        }
                    case CameraProperties.CameraZooming:
                        {
                            Game1.viewMatrix = Matrix.CreateLookAt(CameraProperties.cameraPosition, Game1.newDisc.BasketPosition + new Vector3(0, 3f, 0), Vector3.Up);
                            CameraProperties.View = Game1.viewMatrix;
                            break;
                        }
                    case CameraProperties.CameraNotMoving:
                        {

                            Game1.viewMatrix = Matrix.CreateLookAt(Advanced_Terrain.CameraProperties.cameraPosition, Game1.newDisc.Position, Vector3.Up);
                            CameraProperties.View = Game1.viewMatrix;
                            break;
                        }

                }
                if (Game1.GameState != Game1.GameStateMenu)
                {
                    Vector3 reflCameraPosition = Advanced_Terrain.CameraProperties.cameraPosition;
                    reflCameraPosition.Y = -Advanced_Terrain.CameraProperties.cameraPosition.Y + Game1.newTerrain.waterHeight * 2;
                    Vector3 reflTargetPos = Game1.newDisc.Position;
                    reflTargetPos.Y = -reflTargetPos.Y + Game1.newTerrain.waterHeight * 2;
                    Vector3 cameraRight = Vector3.Right;
                    Vector3 invUpVector = Vector3.Cross(cameraRight, reflTargetPos - reflCameraPosition);
                    Game1.reflectionViewMatrix = Matrix.CreateLookAt(reflCameraPosition, reflTargetPos, invUpVector);
                }
            
          

        }

        public Quaternion GetWiiMoteRotation()
        {
           return MyWiimote.Rotation;
        }
        public Boolean WiiMoteConnected()
        {
            return MyWiimote.IsConnected;
        }
        public Boolean InitWiimote()
        {
            try
            {
                MyWiimote.InitWiiMote();
            }
            catch
            {
                return false;

            }
            return true;
        }
    }
}